package com.itboy.mall.pms.attrgroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.pms.attr.service.IMallAttrService;
import com.itboy.mall.pms.attr.vo.MallAttrVo;
import com.itboy.mall.pms.attrattrgroup.domain.MallAttrAttrGroup;
import com.itboy.mall.pms.attrattrgroup.service.IMallAttrAttrGroupService;
import com.itboy.mall.pms.category.domain.MallCategory;
import com.itboy.mall.pms.category.service.IMallCategoryService;
import com.itboy.mall.pms.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupAddBo;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupQueryBo;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupEditBo;
import com.itboy.mall.pms.attrgroup.domain.MallAttrGroup;
import com.itboy.mall.pms.attrgroup.mapper.MallAttrGroupMapper;
import com.itboy.mall.pms.attrgroup.vo.MallAttrGroupVo;
import com.itboy.mall.pms.attrgroup.service.IMallAttrGroupService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

import static com.itboy.mall.pms.attr.constant.AttrEnum.MALL_ATTR_SPEC;

/**
 * 属性分组Service业务层处理
 *
 * @author itboy
 * @date 2021-07-17
 */
@Service
public class MallAttrGroupServiceImpl extends ServicePlusImpl<MallAttrGroupMapper, MallAttrGroup> implements IMallAttrGroupService {

	@Autowired
	private IMallCategoryService iMallCategoryService;

	@Autowired
	private IMallAttrGroupService iMallAttrGroupService;

	@Autowired
	private IMallAttrAttrGroupService iMallAttrAttrGroupService;

	@Autowired
	private IMallAttrService iMallAttrService;

    @Override
    public MallAttrGroupVo queryById(Long attrGroupId){
        return getVoById(attrGroupId, MallAttrGroupVo.class);
    }

	@Override
	public List<MallAttrGroupVo> getListByCatelogId(Long catalogId) {
		LambdaQueryWrapper<MallAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallAttrGroup::getCatalogId,catalogId);
		List<MallAttrGroupVo> mallAttrVoList = iMallAttrGroupService.listVo(queryWrapper, MallAttrGroupVo.class);
		return mallAttrVoList;
	}
	@Override
    public TableDataInfo<MallAttrGroupVo> queryPageList(MallAttrGroupQueryBo bo) {
        PagePlus<MallAttrGroup, MallAttrGroupVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallAttrGroupVo.class);
		List<MallAttrGroupVo> recordsVo = result.getRecordsVo();
		//分类id
		List<Long> collect = recordsVo.stream().map(e -> e.getCatalogId()).collect(Collectors.toList());
		if(CollectionUtil.isNotEmpty(collect)){
			LambdaQueryWrapper<MallCategory> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(MallCategory::getId,collect);
			List<MallCategory> categoryList = iMallCategoryService.list(queryWrapper);
		    //设置分类名称
			recordsVo.forEach(e->{
				MallCategory mallCategory = categoryList.stream().filter(t -> t.getId().equals(e.getCatalogId())).findFirst().orElse(null);
				if(ObjectUtil.isNotNull(mallCategory)){
					e.setCatalogName(mallCategory.getName());
				}
			});
		}

		return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallAttrGroupVo> queryList(MallAttrGroupQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallAttrGroupVo.class);
    }

    private LambdaQueryWrapper<MallAttrGroup> buildQueryWrapper(MallAttrGroupQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallAttrGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getAttrGroupName()), MallAttrGroup::getAttrGroupName, bo.getAttrGroupName());
        lqw.eq(bo.getCatalogId() != null, MallAttrGroup::getCatalogId, bo.getCatalogId());
        return lqw;
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(MallAttrGroupAddBo bo) {
        MallAttrGroup add = BeanUtil.toBean(bo, MallAttrGroup.class);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallAttrGroupEditBo bo) {
        MallAttrGroup update = BeanUtil.toBean(bo, MallAttrGroup.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallAttrGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		//删除关联属性
		LambdaQueryWrapper<MallAttrAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(MallAttrAttrGroup::getAttrGroupId,ids);
		iMallAttrAttrGroupService.remove(queryWrapper);
        return removeByIds(ids);
    }

	@Override
	public List<MallAttrGroupVo> getAttrGroupWithAttrByCatelogId(Long catalogId) {
    	//1.查询分组数据
		LambdaQueryWrapper<MallAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallAttrGroup::getCatalogId,catalogId);
		List<MallAttrGroupVo> mallAttrGroupList = iMallAttrGroupService.listVo(queryWrapper, MallAttrGroupVo.class);
		//2.按分组id查询属性
		if(CollectionUtil.isNotEmpty(mallAttrGroupList)){
			mallAttrGroupList.forEach(item->{
				List<MallAttrVo> attrVoList = iMallAttrService.getListByGroupId(item.getAttrGroupId(),MALL_ATTR_SPEC.getValue());
				if(CollectionUtil.isNotEmpty(attrVoList)){
					item.setMallAttrVoList(attrVoList);
				}
			});
		}

		return mallAttrGroupList;
	}

	@Override
	public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
		//1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
		List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
		return vos;
	}
}
